/*
 *  Check exist
 */

#include <stdio.h>
#include <string.h>
#include <pulse/pulseaudio.h>
#include <pulse/sample.h>
#include <pulse/simple.h>

int main(int argc, char** argvs)
{
    const char name[] = "test";
    pa_simple *handle = NULL;
    pa_sample_spec ss;
    int error;

    if (argc > 1 && (strstr(argvs[1], "-?") || strstr(argvs[1], "help")))
    {
        printf("\r\n");
        printf("Usage: %s chn[default:2] freq[default:44100] fmt[default:0/LE 1/BE] \r\n", argvs[0]);
        printf("\r\n");
        printf("Example: %s 2 44100 \r\n", argvs[0]);
        printf("\r\n");
        return 0;
    }

    ss.channels = argc > 1 ? (uint8_t)atoi(argvs[1]) : 2;
    ss.rate = argc > 2 ? (uint32_t)atoi(argvs[2]) : 44100;
    ss.format = (argc > 3 && atoi(argvs[3])) ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE;

    printf("Init Test: chn:%d, freq:%d, format:S16%s \r\n",
        ss.channels,
        ss.rate,
        ss.format == PA_SAMPLE_S16LE ? "LE" : "BE");

    handle = pa_simple_new(NULL, name, PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error);
    if (!handle)
    {
        printf("pa_simple_new playback failed ! \r\n");
    }
    else
    {
        printf("pa_simple_new playback success \r\n");
        pa_simple_free(handle);
    }

    handle = pa_simple_new(NULL, name, PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error);
    if (!handle)
    {
        printf("pa_simple_new record failed ! \r\n");
    }
    else
    {
        printf("pa_simple_new record success \r\n");
        pa_simple_free(handle);
    }

    return 0;
}
